<?php
namespace PsiFramework\Classes\Util\Object;

use PsiFramework\Classes\Model\Object;

use \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 *
 *
 * @package Util.Parameters
 */
class ReferenceAttribute implements IPrimitiveObject
{

    /**
     * 
     * @var string
     */
    protected /*string*/ $_objectType ;
    
    /**
     * 
     * @var string
     */
    protected /*string*/  $_attribute ;

    /**
     * 
     * @param string $objectType
     * @param string $attribute
     */
    public function __construct(/*string*/ $objectType=null, /*string*/ $attribute=null)
    {
        $this->_objectType = $objectType ;
        $this->_attribute = $attribute ;
    }

    /**
     * 
     * @return string
     */
    public function getObjectType()
    {
        return $this->_objectType ;
    }

    /**
     * 
     * @param string $objectType
     */
    public function setObjectType(/*string*/ $objectType)
    {
        $this->_objectType = $objectType ;
    }

    /**
     * 
     * @return string
     */
    public function getAttribute()
    {
        return $this->_attribute ;
    }

    /**
     * 
     * @param string $attribute
     */
    public function setAttribute(/*string*/ $attribute)
    {
        $this->_attribute = $attribute ;
    }

    /**
     * 
     * @param Object $obj
     * 
     * @return string
     */
    public function getValue(Object $obj=null)
    {
        if (is_null($obj) || isNullOrEmptyString($this->_attribute)) {
            return '';
        }
        
        if (is_null($this->_objectType) || $obj instanceof $this->_objectType) {
            try {
                $getter = 'get'.ucfirst($this->_attribute);
                return $obj->{$getter}();
            } catch (\Exception $e) {
                return '';
            }
        } else {
            return '';
        }
    }

    /**
     * (non-PHPdoc)
     * @see \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject::serialize()
     */
    public function serialize(/*boolean*/ $asText=true)
    {
        $json = new \stdClass();
        $json->objectType = $this->_objectType ;
        $json->attribute = $this->_attribute ;

        $jsonObject = new Json($json);

        return $jsonObject->serialize($asText);
    }

    /**
     * (non-PHPdoc)
     * @see \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject::unserialize()
     */
    public function unserialize($value)
    {
        $jsonObject = new Json();
        $jsonObject->unserialize($value);

        $this->_type = $jsonObject->getObject()->objectType ;
        $this->_attribute = $jsonObject->getObject()->attribute ;
    }

    /**
     * (non-PHPdoc)
     * @see \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject::toString()
     */
    public function toString()
    {
        
    }

    /**
     * 
     */
    public static function getSqlType()
    {
        return SqlType::$text ;
    }
}
